package stark.app.function.print;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.stark.camera.kit.id.IdCameraCallback;
import com.stark.camera.kit.id.IdCameraFactory;
import com.stark.doc.selector.DefDocSelActivity;
import com.stark.pdf.lib.model.IPdfCreateListener;
import com.stark.pdf.lib.model.PdfUtil;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;
import com.stark.print.lib.Printer;
import com.stark.print.lib.db.PrintDbHelper;
import com.stark.print.lib.db.PrintRecord;
import com.stark.print.lib.db.PrintType;
import com.stark.print.lib.material.PrintMaterialCategory;
import com.stark.print.lib.material.PrintMaterialProvider;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import stark.app.R;
import stark.app.databinding.ActivityPrintDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.constant.Extra;
import stark.common.basic.media.MediaInfo;

/**
 * Author：daizhongbin on 2022/7/15
 * Email：1027154338@qq.com
 * Desc:
 */
public class PrintDemoActivity extends BaseNoModelActivity<ActivityPrintDemoBinding> {

    @Override
    protected int onCreate() {
        return R.layout.activity_print_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnPrintImg.setOnClickListener(v -> {
            printImg();
        });
        mDataBinding.btnPrintImg1.setOnClickListener(v -> {
            printImgs();
        });
        mDataBinding.btnPrintDoc.setOnClickListener(v -> {
            selDoc();
        });
        mDataBinding.btnPrintWeb.setOnClickListener(v -> {
            printWeb();
        });
        mDataBinding.btnIdCard.setOnClickListener(v -> {
            printIdCard();
        });
        mDataBinding.btnIdDrive.setOnClickListener(v -> {
            printIdDrive();
        });
        mDataBinding.btnIdYyzz.setOnClickListener(v -> {
            printIdYyzz();
        });

        mDataBinding.btnGetMaterial.setOnClickListener(v -> {
            getPrintMaterial();
        });
        mDataBinding.btnGetRecords.setOnClickListener(v -> {
            getRecords();
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        if (requestCode == REQ_SEL_DOC) {
            if (data == null) return;
            MediaInfo mediaInfo = (MediaInfo) data.getSerializableExtra(Extra.DATA);
            printDoc(mediaInfo);
        }
    }

    private void printImg() {
        PhotoSelectorBuilder.builder(this)
                .mode(PhotoConfig.Mode.PHOTO)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        if (data == null || data.size() == 0) return;
                        SelectMediaEntity entity = data.get(0);
                        String jobName = entity.getMediaName();
                        Uri uri = Uri.parse(entity.getUri());

                        /**
                         * 请注意：要确保能打印，请确保打印机为支持wifi的打印机，同时请确保手机跟打印机连接的是同一个wifi热点
                         */
                        Printer.printBitmap(PrintDemoActivity.this, jobName, uri);
                    }
                });

    }

    private void printImgs() {
        PhotoSelectorBuilder.builder(this)
                .mode(PhotoConfig.Mode.PHOTO)
                .maxCount(110)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        if (data == null || data.size() == 0) {
                            return;
                        }

                        List<String> imgPathList = new ArrayList<>();
                        for (SelectMediaEntity entity : data) {
                            imgPathList.add(entity.getPath());
                        }
                        PdfUtil.img2Pdf(imgPathList, new IPdfCreateListener() {
                            @Override
                            public void onStart() {
                                showDialog("转换中...");
                            }

                            @Override
                            public void onEnd(boolean success, String path) {
                                dismissDialog();
                                ToastUtils.showShort(success ? "转换成功" : "转换失败");
                                String jobName = FileUtils.getFileNameNoExtension(path);
                                Printer.printDoc(PrintDemoActivity.this, jobName, UriUtils.file2Uri(new File(path)));
                            }
                        });

                    }
                });

    }

    private void printWeb() {
        PrintWebActivity.start(this, "https://www.baidu.com/");
    }

    private static final int REQ_SEL_DOC = 1;

    private void selDoc() {
        DefDocSelActivity.selFile(this, REQ_SEL_DOC);
    }

    private void printDoc(MediaInfo docInfo) {
        String jobName = docInfo.getName();
        Uri uri = Uri.parse(docInfo.getUri());
        Printer.printDoc(this, jobName, uri);
    }

    private void getPrintMaterial() {
        List<PrintMaterialCategory> categoryList = PrintMaterialProvider.getPrintMaterialCategories();
        Log.e("dzb", "categoryList = " + GsonUtils.toJson(categoryList));
    }

    private void getRecords() {
        //获取所有的打印记录
        //List<PrintRecord> records = PrintDbHelper.getAll();

        //根据类型获取打印记录
        List<PrintRecord> records = PrintDbHelper.getByType(PrintType.PDF);
        Log.e("dzb", "records = " + GsonUtils.toJson(records));
        if (records == null || records.size() == 0) {
            ToastUtils.showShort("没有打印记录");
        } else {
            ToastUtils.showShort("获取到了" + records.size() + "条打印记录");
        }
    }

    private void printIdCard() {
        IdCameraFactory.takeIdCard(this, new IdCameraCallback() {
            @Override
            public void onResult(List<Bitmap> bitmaps) {
                PrintIdActivity.start(PrintDemoActivity.this, bitmaps, PrintIdActivity.TYPE_ID_CARD);
            }
        });
    }

    private void printIdYyzz() {
        IdCameraFactory.takeIdYyzz(this, new IdCameraCallback() {
            @Override
            public void onResult(List<Bitmap> bitmaps) {
                PrintIdActivity.start(PrintDemoActivity.this, bitmaps, PrintIdActivity.TYPE_ID_YYZZ);
            }
        });
    }

    private void printIdDrive() {
        IdCameraFactory.takeIdDrive(this, new IdCameraCallback() {
            @Override
            public void onResult(List<Bitmap> bitmaps) {
                PrintIdActivity.start(PrintDemoActivity.this, bitmaps, PrintIdActivity.TYPE_ID_DRIVE);
            }
        });
    }
}
